package com.kaba.gzlcmxgl.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaba.bean.*;
import com.kaba.bean.mongodb.ZuoPin;
import com.kaba.zplb.service.WorksService;
import org.activiti.engine.*;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Date 2021/3/15
 * @Description TODO
 */
@RestController
@RequestMapping("exclusiveactiviti")
@CrossOrigin
public class PaitawangguanController {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private WorksService worksService1;

    @Autowired
    private ProcessEngine processEngine;

    //流程排他部署
    @RequestMapping(value = "/exclusivestartProcess")
    @ResponseBody
    public ResponseResult exclusivestartProcess() {
        //进行部署
        Deployment deployment = repositoryService.createDeployment()
                //添加bpmn资源
                .addClasspathResource("processes/paita01.bpmn")
                .addClasspathResource("processes/paita01.png")
                .name("颁奖流程")
                .deploy();
        //输出部署一些信息
        System.out.println("流程部署id:" + deployment.getId());
        System.out.println("流程部署名称:" + deployment.getName());



        return ResponseResult.success();
    }

    /*
     * 排他流程定义
     * */
    @GetMapping(value = "/exclusivedefinition")
    public ResponseResult exclusivedefinition(String zuopinid,String username) {
        //前端传入的金额装入
        Map<String,Object> map11=new HashMap<String,Object>();
        map11.put("username",username);
        map11.put("tianqi",username);
        map11.put("zhaoliu",username);
        System.out.println("BusinessKey:"+zuopinid);
        System.out.println("当前负责人:"+username);
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //流程定义完成
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess01", zuopinid,map11);
        //根据businesskey查询当前申请的作品信息，根据作品id 修改作品表的状态为 4
        System.out.println("新定义的流程："+processInstance);
        //1,根据负责人，查询对象task
        List<Task> task = taskService.createTaskQuery().taskAssignee(username).list();
        //2.使用任务ID，获取实例ID
        for (Task task1 : task) {
            //流程实例id
            String processInstanceId = task1.getProcessInstanceId();
            //3.使用流程实例id，查询当前正在执行的任务节点
            ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            //获取business_key
            String business_key = pi.getBusinessKey();
            System.out.println("business_key主键id："+business_key);
            //根据业务表id查询当前业务信息
            ZuoPin work = worksService1.queryById(business_key);
            System.out.println("当前申请的业务id："+work.getZuopinid());
            System.out.println("当前申请的作品名称："+work.getTitle());
            System.out.println("当前审批状态："+work.getStates());
            System.out.println("流程实例id："+processInstanceId);
            //修改审批状态state
            //修改审批状态state
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("zuopinid",work.getZuopinid());
            updateWrapper.set("states","4");
            worksService1.update(updateWrapper);
            processEngine.getTaskService()//管理员进行名次申请
                    .complete(task1.getId());

        }


        return ResponseResult.success();
    }


    /**完成我的任务*/
    @GetMapping(value = "/exclusivemycomplete")
    @CrossOrigin
    @ResponseBody
    public ResponseResult exclusivemycomplete(String daibanid,String num){
        System.out.println("实例id："+daibanid);

        Map<String,Object> map=new HashMap<>();
        map.put("num",num);
        //1.传入实例id查询出当前task对象
        List<Task> taskList=taskService.createTaskQuery().processInstanceId(daibanid).list();
        //2.使用流程实例，查询
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(daibanid).singleResult();
        //3.获取business_key
        String business_key = pi.getBusinessKey();
        System.out.println("业务主键id:"+business_key);
        System.out.println("完成前："+taskList);

        for (Task task : taskList) {
            processEngine.getTaskService()//与正在执行的任务管理相关的Service
                    .complete(task.getId(),map);
        }
        //完成任务之后再次查询
        List<Task> taskList1=taskService.createTaskQuery().processInstanceId(daibanid).list();
        System.out.println("完成后："+taskList1);
        if (taskList1.size()==0){
            //根据业务表id查询当前业务信息
            ZuoPin work = worksService1.queryById(business_key);
            System.out.println("当前申请的业务id："+work.getZuopinid());
            System.out.println("当前申请的作品名称："+work.getTitle());
            System.out.println("当前审批状态："+work.getStates());
            //修改审批状态state
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("zuopinid",work.getZuopinid());
            updateWrapper.set("states","5");
            worksService1.update(updateWrapper);

        }


        return ResponseResult.success(daibanid);
    }
    /*
     * 驳回
     * */
    @RequestMapping("/deleteComplete")
    public ResponseResult deleteComplete(String daibanid,String Suggestions){
        Suggestions="不通过";
        //1.传入实例id查询出当前task对象
        List<Task> taskList=taskService.createTaskQuery().processInstanceId(daibanid).list();
        //2.使用流程实例，查询
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(daibanid).singleResult();
        //3.获取business_key
        String business_key = pi.getBusinessKey();
        System.out.println("业务主键id:"+business_key);
        System.out.println("流程实例id:"+daibanid);
        System.out.println("流程实例id:"+daibanid);
        //根据流程实例id删除当前流程
        runtimeService.deleteProcessInstance(daibanid,Suggestions);
        //修改状态
        ZuoPin work = worksService1.queryById(business_key);
        //修改审批状态state
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("zuopinid",work.getZuopinid());
        updateWrapper.set("states","3");
        worksService1.update(updateWrapper);
        return ResponseResult.success();
    }

    }
